package com.cleargroup.magic.cleara.c.command;

import android.provider.MediaStore;

import java.util.List;

/**
 * Created by Administrator on 2016/7/12 0012.
 */
public class StoreManagerQueryHelper implements MediaStore.Files.FileColumns {

    static String and() {
        return " and ";
    }

    static String or() {
        return " or ";
    }

    static String paren(String input) {
        return "(" + input + ")";
    }

    static String likeOrNot(String column, boolean like) {
        return column + (like ? " like ?" : " not like ?");
    }

    static String dataLikeX() {
        return likeOrNot(DATA, true);
    }

    static String dataLikeX(String[] array) {
        return dataLikeX(array.length);
    }

    static String dataLikeX(int count) {
        return dataLikeXAndOr(count, or());
    }

    static String dataNotLikeX() {
        return likeOrNot(DATA, false);
    }

    static String dataNotLikeX(String[] array) {
        return dataNotLikeX(array.length);
    }

    static String dataNotLikeX(int count) {
        return dataNotLikeXAndOr(count, and());
    }

    static String dataLikeXAndOr(int count, String andOr) {
        String res = "";

        for(int i = 0; i < count; i++) {
            res = res + dataLikeX();
            if(i < count - 1) {
                res = res + andOr;
            }
        }

        return paren(res);
    }

    static String dataNotLikeXAndOr(int count, String andOr) {
        String res = "";

        for(int i = 0; i < count; i++) {
            res = res + dataNotLikeX();
            if(i < count - 1) {
                res = res + andOr;
            }
        }

        return paren(res);
    }

    static String[] concatArrays(String[] a, String[] b, String[] c) {
        return concatArrays(concatArrays(a, b), c);
    }

    static String[] concatArrays(String[] a, String[] b) {
        String[] res = null;

        if(a != null && b != null) {
            res = new String[a.length + b.length];
            System.arraycopy(a, 0, res, 0, a.length);
            System.arraycopy(b, 0, res, a.length, b.length);
        } else if(a != null && b == null) {
            res = a;
        } else if(a == null && b != null) {
            res = b;
        }

        return res;
    }

    static String[] containsPatterns(List<String> list) {
        String[] res = new String[list.size()];
        for(int i = 0; i < list.size(); i++) {
            res[i] = "%" + list.get(i) + "%";
        }
        return res;
    }

    static String columnNotInColumn(String c1, String c2) {
        return c1 + " not in " + paren("select distinct " + c2 + " from files");
    }
}
